

package at.fhj.itm.metadata.impl.postgres;


import at.fhj.itm.metadata.Constraint;
import at.fhj.itm.metadata.impl.AbstractTable;
import static at.fhj.itm.metadata.MetadataUtil.copyMetadataList;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 *
 * @author Seuchter
 */
public class PostgresTable extends  AbstractTable implements
    PostgresMetaDataItem, Comparable<PostgresTable>{

    private long oid;
    private PostgresUser owner;
    private List<PostgresColumn> columns;
    private List<PostgresIndex> indices;
    private List<PostgresConstraint> constraints;
    private List<PostgresTrigger> trigger;


    private static final List<String> REFERENCE_LESS_CONSTRAINTS;

    static
    {
        REFERENCE_LESS_CONSTRAINTS = new ArrayList<String>();
        REFERENCE_LESS_CONSTRAINTS.add(Constraint.CONSTRAINT_TYPE_CHECK);
        //REFERENCE_LESS_CONSTRAINTS.add(Constraint.CONSTRAINT_TYPE_NOT_NULL);
        REFERENCE_LESS_CONSTRAINTS.add(Constraint.CONSTRAINT_TYPE_PRIMARY_KEY);
        REFERENCE_LESS_CONSTRAINTS.add(Constraint.CONSTRAINT_TYPE_UNIQUE);
    }

    public PostgresTable(long oid, String name, PostgresUser owner)
    {
        super(name);
        this.oid = oid;
        this.owner = owner;

        columns = new ArrayList<PostgresColumn>();
        indices = new ArrayList<PostgresIndex>();
        constraints = new ArrayList<PostgresConstraint>();

    }


    public PostgresUser getOwner()
    {
        return this.owner;
    }

    public long getOid()
    {
        return oid;
    }
    public void setConstraints(List<PostgresConstraint> constraints){
        this.constraints.addAll(constraints);
    }
    public  void setColumns(List<PostgresColumn> columns)
    {
        this.columns.addAll(columns);
    }
    public  void setIndices(List<PostgresIndex> indices)
    {
        this.indices.addAll(indices);
    }
    public  void setTriggers(List<PostgresTrigger> triggers)
    {
        this.trigger = triggers;
    }
    @Override
    public String toString()
    {
        return String.format("[PostgresTable oid: %d name:%s children: %d]", getOid(), getName(), getChildren().size());
    }

    @Override
    public List<PostgresColumn> getColumns()
    {
        return columns;
    }

    @Override
    public List<PostgresIndex> getIndices()
    {
        return indices;
    }

    @Override
    public List<PostgresConstraint> getConstraints()
    {
        return constraints;
    }
    @Override
    public List<PostgresTrigger> getTriggers()
    {
        return trigger;
    }

    protected void setConstraint(List<PostgresConstraint> constraints){
        this.constraints = constraints;
    }

    public int compareTo(PostgresTable t)
    {
        return (int) (getOid() - t.getOid());
    }

    public List<PostgresConstraint> getRefeferenceLessConstraints()
    {
        List<PostgresConstraint> cols = new ArrayList<PostgresConstraint>();

        for(PostgresConstraint constraint : getConstraints())
        {
            String constType = constraint.getConstraintType();

            if(REFERENCE_LESS_CONSTRAINTS.contains(constType))
            {
                cols.add(constraint);
            }
        }

        return Collections.unmodifiableList(cols);
    }

    public PostgresTable copy(){
        PostgresTable tbl = new PostgresTable(-1, getName(), getOwner());
        tbl.setColumns(copyMetadataList(getColumns()));
        tbl.setConstraint(copyMetadataList(getConstraints()));
        tbl.setIndices(copyMetadataList(getIndices()));
        tbl.setTriggers(copyMetadataList(getTriggers()));

        return tbl;

    }



}
